package com.puppet.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.puppet.RedisConst;
import com.puppet.pojo.BookType;
import com.puppet.service.BookTypeService;
import com.puppet.mapper.BookTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author Kai
* @description 针对表【t_book_type】的数据库操作Service实现
* @createDate 2025-07-25 10:57:18
*/
@Service
public class BookTypeServiceImpl extends ServiceImpl<BookTypeMapper, BookType>
    implements BookTypeService{
    @Autowired
    private BookTypeMapper bookTypeMapper;


    /*这是一种新写法，可以不用导入mapper的依赖，直接使用baseMapper也可以识别，底层已经有导入依赖了*/
    @Override
    @Cacheable(cacheNames = RedisConst.BOOK_TYPE_LIST, keyGenerator = "keyGenerator")
    public List<BookType> findAll() {
        return baseMapper.selectList(null);
    }

    @Override
    public List<Integer> findByTableId() {
        //就算只查询id字段，返回的也是BookType对象,这里的wrapper只是条件构造器，不是返回的结果
        Wrapper<BookType> wrapper = Wrappers.lambdaQuery(BookType.class)
                .select(BookType::getId);
        //这里才是
        List<BookType> bookTypes = bookTypeMapper.selectList(wrapper);
        //对结果进行转换，转为数字集合
        List<Integer> typeIds = bookTypes.stream()
                .map(BookType::getId)
                .collect(Collectors.toList());
        return typeIds;
    }
}




